package com.chuanye.jiguangimdemo.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanye.jiguangimdemo.R;

import java.util.List;

import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.api.BasicCallback;

public class ChatAdapter extends BaseAdapter {
    private Context mContext;
    private Conversation conversation;
    private List<Message> messageList;


    //当前第0项消息的位置
    private int mStart;

    //文本
    private final int TYPE_SEND_TXT = 0;
    private final int TYPE_RECEIVE_TXT = 1;//收到


    public ChatAdapter(Context mContext, Conversation conversation) {
        this.mContext = mContext;
        this.conversation = conversation;
        this.messageList = conversation.getAllMessage();

        //this.messageList = conversation.getMessagesFromNewest(0, 100);
        for (int i = 0; i < messageList.size(); i++){
            if (ContentType.text == messageList.get(i).getContentType()){
                TextContent textContent = (TextContent) messageList.get(i).getContent();
                Log.i("打印消息11：",""+textContent.getText());
            }
        }
    }

    @Override
    public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        return messageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 获取返回类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        Message msg = messageList.get(position);
        switch (msg.getContentType()){
            case text:
                return msg.getDirect() == MessageDirect.send ? TYPE_SEND_TXT
                        : TYPE_RECEIVE_TXT;
        }


        return super.getItemViewType(position);
    }

    //获取返回类型的总数量
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        ContentType contentType = messageList.get(position).getContentType();
        if (convertView == null){
            //if (contentType == ContentType.text ) {//文本
                if (MessageDirect.receive == messageList.get(position).getDirect()) {//接收
                    convertView = View.inflate(mContext, R.layout.item_chat_receive_view,null);
                }else if (MessageDirect.send == messageList.get(position).getDirect()) {
                    convertView = View.inflate(mContext, R.layout.item_chat_send_view,null);
                }
           // }

            holder = new ViewHolder();

            holder.item_chat_text_icon_iv = convertView.findViewById(R.id.item_chat_text_icon_iv);
            holder.item_chat_text_content_tv = convertView.findViewById(R.id.item_chat_text_content_tv);


            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }


        //if (contentType == ContentType.text ){//文本
            holder.item_chat_text_icon_iv.setImageResource(R.drawable.rc_default_portrait);

            TextContent textContent = (TextContent) messageList.get(position).getContent();
            holder.item_chat_text_content_tv.setText(textContent.getText());

            Log.i("打印消息item:",""+textContent.getText());

        //}


        return convertView;
    }

    public static class ViewHolder{
        LinearLayout itemchat_receive_text_ll;

        ImageView item_chat_text_icon_iv;
        TextView item_chat_text_content_tv;
        LinearLayout item_chat_send_text_ll;

    }


    /**
     * 添加消息
     * @param msg
     */
    public void addMsgFromReceiptToList(Message msg) {
        messageList.add(msg);
        //消息发送结果监听
        msg.setOnSendCompleteCallback(new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                    // //当有新消息加到MsgList，自增mStart
                    //incrementStartPosition();
                    notifyDataSetChanged();
                } else {
                    //HandleResponseCode.onHandle(mContext, i, false);
                    notifyDataSetChanged();
                }
            }
        });
    }

    //当有新消息加到MsgList，自增mStart
    private void incrementStartPosition() {
        ++mStart;
    }


}
